package com.ruoyi.project.channel.payerRate.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.payerRate.domain.PayerRate;
import com.ruoyi.project.channel.payerRate.service.IPayerRateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 支付商费率信息操作处理
 *
 * @author zero
 * @date 2019-05-12
 */
@Controller
@RequestMapping("/channel/payerRate")
public class PayerRateController extends BaseController {
    private String prefix = "channel/payerRate";

    @Autowired
    private IPayerRateService payerRateService;

    @RequiresPermissions("channel:payerRate:view")
    @GetMapping("/{payerUserId}")
    public String payerRate(@PathVariable("payerUserId") Long payerUserId, ModelMap mmap) {
        mmap.put("payerUserId", payerUserId);
        return prefix + "/payerRate";
    }

    /**
     * 查询支付商费率列表
     */
    @RequiresPermissions("channel:payerRate:list")
    @PostMapping("/list/{payerUserId}")
    @ResponseBody
    public TableDataInfo list(PayerRate payerRate, @PathVariable("payerUserId") Long payerUserId) {
        startPage();
        payerRate.setPayerUserId(payerUserId);
        List<PayerRate> list = payerRateService.selectPayerRateList(payerRate);
        return getDataTable(list);
    }


    /**
     * 导出支付商费率列表
     */
    @RequiresPermissions("channel:payerRate:export")
    @PostMapping("/export/{payerUserId}")
    @ResponseBody
    public AjaxResult export(PayerRate payerRate, @PathVariable("payerUserId") Long payerUserId) {
        payerRate.setPayerUserId(payerUserId);
        List<PayerRate> list = payerRateService.selectPayerRateList(payerRate);
        ExcelUtil<PayerRate> util = new ExcelUtil<PayerRate>(PayerRate.class);
        return util.exportExcel(list, "payerRate");
    }

    /**
     * 新增支付商费率
     */
    @GetMapping("/add/{payerUserId}")
    public String add(@PathVariable("payerUserId") Long payerUserId, ModelMap mmap) {
        mmap.put("payerUserId", payerUserId);
        return prefix + "/add";
    }

    /**
     * 新增保存支付商费率
     */
    @RequiresPermissions("channel:payerRate:add")
    @Log(title = "支付商费率", businessType = BusinessType.INSERT)
    @PostMapping("/add/{payerUserId}")
    @ResponseBody
    public AjaxResult addSave(PayerRate payerRate,@PathVariable("payerUserId") Long payerUserId) {

        PayerRate search = new PayerRate();
        search.setPayerUserId(payerUserId);
        search.setPayType(payerRate.getPayType());
        List<PayerRate> rates = payerRateService.selectPayerRateList(search);
        if(rates.size() > 0){
            rates.get(0).setPayRate(payerRate.getPayRate());
            return toAjax(payerRateService.updatePayerRate(rates.get(0)));
        }
        payerRate.setPayerUserId(payerUserId);
        return toAjax(payerRateService.insertPayerRate(payerRate));
    }

    /**
     * 修改支付商费率
     */
    @GetMapping("/edit/{rateId}")
    public String edit(@PathVariable("rateId") Long rateId, ModelMap mmap) {
        PayerRate payerRate = payerRateService.selectPayerRateById(rateId);
        mmap.put("payerRate", payerRate);
        return prefix + "/edit";
    }

    /**
     * 修改保存支付商费率
     */
    @RequiresPermissions("channel:payerRate:edit")
    @Log(title = "支付商费率", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PayerRate payerRate) {
        return toAjax(payerRateService.updatePayerRate(payerRate));
    }

    /**
     * 删除支付商费率
     */
    @RequiresPermissions("channel:payerRate:remove")
    @Log(title = "支付商费率", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(payerRateService.deletePayerRateByIds(ids));
    }

}
